package com.example.PDFdemo.pdftest;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class PDFUtil {

/*
    //itext7
    //根据表单域添加文字
    public static void addWork(String oldFilePath, String newFilePath, Map<String,String> contextMap) throws IOException {
        //创建PDF文档对象
        PdfDocument document = new PdfDocument(new PdfReader(oldFilePath), new PdfWriter(newFilePath));
        //设置字体
        PdfFont font = PdfFontFactory.createFont("/Inkfree.ttf");
        //设置字体颜色
        DeviceRgb color = new DeviceRgb(0,0,0);
        //获取PDF文件中的表单域
        PdfAcroForm form = PdfAcroForm.getAcroForm(document, true);
        //填充文本
        for (Map.Entry<String, String> entry : contextMap.entrySet()) {
            form.getFormFields().get(entry.getKey()).setValue(entry.getValue()).setColor(color).setFont(font);
        }
        //清楚表单域样式
        form.flattenFields();
        document.close();
    }
*/

    public static void pdfAddText(String oldFilePath, String newFilePath, List<PDFModel> models) throws DocumentException, IOException {
        //获取文件
        PdfReader reader = new PdfReader(oldFilePath);
        int n = reader.getNumberOfPages();
        System.out.println("总页数：" + reader.getNumberOfPages());
        List weights = new ArrayList<>();
        for (int i = 1; i <= n; i++) {
            System.out.println("第" + i + "页");
            float width = (float) Math.ceil(reader.getPageSizeWithRotation(i).getRight());
            float height = (float) Math.ceil(reader.getPageSizeWithRotation(i).getHeight());
            System.out.println("宽：" + width);
            System.out.println("高：" + height);
            width = (float) (width * (72 / 25.4));
            height = (float)(height * (72 / 25.4));
            System.out.println("宽 * (72 / 25.4)：" + width);
            System.out.println("高 * (72 / 25.4)：" + height);
            weights.add(width);
        }
        float maxW = (float) Collections.max(weights);
        float pageMaxWidth;
        if (maxW > 800) {
            pageMaxWidth = 800;
        } else {
            pageMaxWidth = maxW;
        }
        float radius = pageMaxWidth / maxW;
        System.out.println("radius：" + radius);
        for (int i = 1; i <= n; i++) {
            float width = (float) Math.ceil(reader.getPageSizeWithRotation(i).getRight());
            float height = (float) Math.ceil(reader.getPageSizeWithRotation(i).getHeight());
            width = (float) (width * (72 / 25.4));
            height = (float)(height * (72 / 25.4));
            width = width * radius;
            height = height * radius;
            System.out.println("第" + i + "页");
            System.out.println("宽 * radius：" + width);
            System.out.println("高 * radius：" + height);
        }
        FileOutputStream outputStream = new FileOutputStream(newFilePath);
        PdfStamper stamper = new PdfStamper(reader, outputStream);
        PdfGState gs = new PdfGState();
        //设置字体
        //BaseFont font =  BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        //插入文本
        PdfContentByte content = null;
        for (PDFModel model : models) {
            String fontPath = "static/" + model.getFontName() + ".ttf";
            BaseFont font = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            content = stamper.getOverContent(model.getPageNumber());
            content.beginText();
            content.setGState(gs);
            content.setColorFill(new BaseColor(0,0,0));
            content.setFontAndSize(font, model.getFontSize());
            content.showTextAligned(Element.ALIGN_LEFT, model.getContext(), model.getX(), model.getY(), 0);
            content.endText();
        }
        stamper.close();
        outputStream.close();
        reader.close();
    }

    public static void pdfAddTexts(String oldFilePath, String newFilePath, List<PDFModel> models) throws DocumentException, IOException {
        //获取文件
        PdfReader reader = new PdfReader(oldFilePath);
        FileOutputStream outputStream = new FileOutputStream(newFilePath);
        PdfStamper stamper = new PdfStamper(reader, outputStream);
        PdfGState gs = new PdfGState();
        //设置字体
        //BaseFont font =  BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        //插入文本
        PdfContentByte content = null;
        for (PDFModel model : models) {
            String fontPath = "static/" + model.getFontName() + ".ttf";
            BaseFont baseFont = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            Font font = new Font(baseFont, model.getFontSize(), Font.ITALIC, BaseColor.BLACK);
            content = stamper.getOverContent(model.getPageNumber());
            ColumnText columnText = new ColumnText(content);
            columnText.setSimpleColumn(100f, 100f, 100f, 100f);
            Paragraph elements = new Paragraph(model.getContext());
            elements.setFont(font);
            columnText.addElement(elements);
            columnText.go();
        }
        stamper.close();
        outputStream.close();
        reader.close();
    }

}
